<?php
class ControllerCatalogFood extends Controller {
    public function index() {
    
        if (isset($this->request->get['id'])) {
            $product_id = (int)$this->request->get['id'];
        } else {
            $product_id = 0;
        }
    
        $this->load->model('catalog/product');
    
        $product_info = $this->model_catalog_product->getProduct($product_id);
        
       
    
        if ($product_info) {
            
            $this->load->model('catalog/review');
    
            $data['product_id'] = $product_id;
            $data['name'] = $product_info['name'];
            	
            $data['quantity'] = $product_info['quantity'];
            	
            $data['price'] = $product_info['price'];
    
            $this->load->model('tool/image');
    
            $data['images'] = array();
    
            $results = $this->model_catalog_product->getProductImages($product_id);
    
            foreach ($results as $result) {
                $data['images'][] = array(
                    'image' => $this->model_tool_image->resize($result['image'], 920, 450)
                );
            }
            	
            if ($product_info['customer_image']) {
                $customer_image = $this->model_tool_image->resize($product_info['customer_image'], $this->config->get('config_customer_image_width'), $this->config->get('config_customer_image_height'));
            } else {
                $customer_image = $this->model_tool_image->resize('no_head_image.jpg', $this->config->get('config_customer_image_width'), $this->config->get('config_customer_image_height'));
            }
            	
            $data['customer_name'] = $product_info['customer_name'];
            $data['customer_image'] = $customer_image;
            $data['customer_rank'] = $product_info['customer_rank'];
            $data['customer_description'] = $product_info['customer_description'];
            $data['address'] = $product_info['address'];
            $data['lat'] = $product_info['lat'];
            $data['lng'] = $product_info['lng'];
            $data['customer_id'] = $product_info['customer_id'];
            $data['times'] = $product_info['times'];
    
            $description = $this->model_catalog_product->getProductAttributeByAttributeId($product_id,$this->config->get('cooker_comment_attribute_id'));
            	
            if($description)
            {
                $data['description'] = $description['text'];
            }
            else
            {
                $data['description'] ="";
            }
            	
            $data['items'] = $this->model_catalog_product->getProductItems($product_id);
            	
            $data['reviews'] = $this->getReviewByCustomer($product_info['customer_id'],1);
            
            $data['is_mobile'] = $this->isMobile() ? 1 : null;
            	
            $this->model_catalog_product->updateViewed($product_id);
    
            $this->response->setHtmlOutput($this->load->view('/food.tpl', $data));
        } else {
            $this->response->setDataResult(0,"美食不存在",null);
        }
    }
    
    protected function getReviewByCustomer($customer_id,$page)
    {
        $this->load->model('catalog/review');
        $this->load->model('tool/image');
         
        $review = array();
         
        $review['reviews'] = array();
         
        $review_total = $this->model_catalog_review->getTotalReviewsByCustomerId($customer_id);
         
        $results = $this->model_catalog_review->getReviewsByCustomerId($customer_id, ($page - 1) * 5, 5);
         
        foreach ($results as $result) {
            if ($result['image']) {
                $customer_image = $this->model_tool_image->resize($result['customer_image'], $this->config->get('config_customer_image_width'), $this->config->get('config_customer_image_height'));
            } else {
                $customer_image = $this->model_tool_image->resize('no_head_image.jpg', $this->config->get('config_customer_image_width'), $this->config->get('config_customer_image_height'));
            }
            $review['reviews'][] = array(
                'author'     => $result['author'],
                'text'       => nl2br($result['text']),
                'rating'     => (int)$result['rating'],
                'customer_image' => $customer_image,
                'date_added' => date($this->config->get('date_format_long'), strtotime($result['date_added']))
            );
        }
         
        $review['total'] = $review_total;
        $review['pageIndex'] = $page;
        $review['pageSize'] = 5;
    
        return $review;
    }
    
    function isMobile()
    
    {
    
        // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
    
        if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
    
        {
    
            return true;
    
        }
    
        // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
    
        if (isset ($_SERVER['HTTP_VIA']))
    
        {
    
            // 找不到为flase,否则为true
    
            return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
    
        }
    
        // 判断手机发送的客户端标志,兼容性有待提高
    
        if (isset ($_SERVER['HTTP_USER_AGENT']))
    
        {
    
            $clientkeywords = array ('nokia',
    
                'sony',
    
                'ericsson',
    
                'mot',
    
                'samsung',
    
                'htc',
    
                'sgh',
    
                'lg',
    
                'sharp',
    
                'sie-',
    
                'philips',
    
                'panasonic',
    
                'alcatel',
    
                'lenovo',
    
                'iphone',
    
                'ipod',
    
                'blackberry',
    
                'meizu',
    
                'android',
    
                'netfront',
    
                'symbian',
    
                'ucweb',
    
                'windowsce',
    
                'palm',
    
                'operamini',
    
                'operamobi',
    
                'openwave',
    
                'nexusone',
    
                'cldc',
    
                'midp',
    
                'wap',
    
                'mobile'
    
            );
    
            // 从HTTP_USER_AGENT中查找手机浏览器的关键字
    
            if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
    
            {
    
                return true;
    
            }
    
        }
    
        // 协议法，因为有可能不准确，放到最后判断
    
        if (isset ($_SERVER['HTTP_ACCEPT']))
    
        {
    
            // 如果只支持wml并且不支持html那一定是移动设备
    
            // 如果支持wml和html但是wml在html之前则是移动设备
    
            if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html'))))
    
            {
    
                return true;
    
            }
    
        }
    
        return false;
    
    }
    
}